<?php

namespace plugin\agent\app\admin\controller;

use app\common\controller\BaseAdminController;
use app\model\pms\Goods;
use plugin\agent\app\admin\logic\AgentGoodsLogic;
use plugin\agent\app\admin\validate\AgentGoodsValidate;
use plugin\agent\app\model\AgentGoods;
use plugin\agent\app\model\AgentRule;
use think\db\Query;

class AgentGoodsController extends BaseAdminController
{
    protected function init()
    {
        parent::init();
        $this->logic = new AgentGoodsLogic();
        $this->validate = new AgentGoodsValidate();
    }

    protected function searchWhere()
    {
        $where = [
            ['is_del', '=', 0]
        ];
        $this->form['name'] && $where[] = ['name', 'like', "%{$this->form['name']}%"];
        $this->form['type'] && $where[] = ['type', '=', $this->form['type']];
        $this->form['cate_id'] && $where[] = ['cate_id', '=', $this->form['cate_id']];

        if ($this->form['tab_type'] == 'sales') {
            //销售中
            $where[] = ['is_show', '=', 1];
        }
        if ($this->form['tab_type'] == 'warehouse') {
            //仓库中
            $where[] = ['is_show', '=', 0];
        }
        if ($this->form['tab_type'] == 'early_warning') {
            //预警中
            $where[] = ['stock', '<=', 20];
        }
        if ($this->form['tab_type'] == 'recovery') {
            //回收站
            $where[] = ['is_del', '=', 1];
        }
        //是否参与分销
        if ($this->form['is_agent'] !== "") {
            $where[] = ['is_agent', '=', $this->form['is_agent']];
        }
        return $where;
    }

    public function list()
    {
        $query = Goods::where($this->searchWhere());
        $query->order("id desc");
        $query->with(array_merge([
            "brand" => function (Query $query) {
                $query->field(['id', 'name']);
            },
        ], Goods::getCateWith()));
        $query->field([
            'id',
            'name',
            'main_image',
            'slider_image',
            'cate_id1',
            'cate_id2',
            'cate_id3',
            'brand_id',
            'min_price',
            'max_price',
        ]);
        $data = $this->logic->getPageList($query);
        $goods_ids = array_column($data['records'], 'id');
        $agentGoods = AgentGoods::where('goods_id', 'in', $goods_ids)
            ->with([
                'rule'
            ])
            ->select()
            ->toArray();
        $agentGoods = array_column($agentGoods, null, 'goods_id');
        $data['records'] = array_map(function ($v) use ($agentGoods) {
            $v['agent'] = $agentGoods[$v['id']] ?? null;
            if ($v['min_price'] == $v['max_price']) {
                $v['price_text'] = $v['min_price'];
            } else {
                $v['price_text'] = $v['min_price'] . '~' . $v['max_price'];
            }
            return $v;
        }, $data['records']);
        return $this->success($data);
    }

    /**
     *
     */
    public function read()
    {
        $id = input('id');
        $goods_id = input('goods_id');
        $info = $this->logic->find($id);
        $query = Goods::where([
            'id' => $goods_id,
            'is_show' => 1,
            'is_del' => 0,
        ]);
        $query->order("id desc");
        $query->with(array_merge([
            "brand" => function (Query $query) {
                $query->field(['id', 'name']);
            },
        ], Goods::getCateWith()));
        $goodsInfo = $query->field([
            'id',
            'name',
            'main_image',
            'slider_image',
            'cate_id1',
            'cate_id2',
            'cate_id3',
            'brand_id',
            'min_price',
            'max_price',
        ])->find();
        if ($info['type'] == 1) {
            $rule = AgentRule::find($info['rule_id']);
            $info['one_rate'] = $rule['one_rate'];
            $info['two_rate'] = $rule['two_rate'];
            $info['three_rate'] = $rule['three_rate'];
        }

        $data['goodsInfo'] = $goodsInfo;
        $data['info'] = $info;


        return $this->success($data);
    }

    /**
     * 参与分销商品
     */
    public function joinAgentGoods()
    {
        $goods_ids = input('goods_ids');
        $this->logic->joinAgentGoods($goods_ids);
        return $this->success();
    }

    /**
     * 取消分销
     */
    public function delAgentGoods()
    {
        $this->validate->checkData(input(), 'del');
        $goods_ids = input("goods_ids");
        if ($goods_ids) {
            $this->logic->delAgentGoods($goods_ids);
        }
        return $this->success();
    }

    /**
     * 修改分销配置
     */
    public function updateAgentGoods()
    {
        $input = input();
        $this->logic->updateAgentGoods($input);
        return $this->success();
    }

}
